﻿using System;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using Miris.HumanMilkAnalyser.DataModel;
using Miris.HumanMilkAnalyser.Service.Interface;
using Miris.HumanMilkAnalyser.Service.Shared;
using Miris.HumanMilkAnalyser.WPF.Model;
using Miris.HumanMilkAnalyser.WPF.Shared;

namespace Miris.HumanMilkAnalyser.WPF.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class CreatePatientViewModel : PatientViewModel
    {
        /// <summary>
        /// Initializes a new instance of the CreatePatientViewModel class.
        /// </summary>
        public CreatePatientViewModel(IDataService dataService)
            : base(dataService) { }

        public override void ResetProperties()
        {
            base.ResetProperties();

            try
            {
                Number = PatientService.GetPatientNumber();

                IsNewData = true;

                MotherName = string.Empty;
                MotherAge = null;
                MotherHeight = null;
                MotherWeight = null;
                MotherBMI = null;
                MotherOpenMilkDate = GetDefaultDate();
                MotherDeliveryMode = "自然顺产";
                MotherEmotion = string.Empty;
                MotherBadHabit = string.Empty;

                BabyName = string.Empty;
                BabyBirthday = GetDefaultDate();
                IsGirl = true;
                IsBabyNotPrematureDelivery = true;
                BabyHeight = null;
                BabyWeight = null;
                BabyHeadCircumference = null;
                BabyBirthHeight = null;
                BabyBirthWeight = null;
                BabyBirthHeadCircumference = null;
                Apgar = null;
            }
            catch (Exception ex)
            {
                RequestDisplayErrorMessage(ex.Message);
            }
        }

        protected override void RegisterMessages()
        {
            base.RegisterMessages();

            RegisterUpdatePatientDataMessage();
        }

        private RelayCommand _saveCommand;

        /// <summary>
        /// Gets the SaveCommand.
        /// </summary>
        public RelayCommand SaveCommand
        {
            get
            {
                return _saveCommand ?? (_saveCommand = new RelayCommand(
                    ExecuteSaveCommand,
                    CanExecuteSaveCommand));
            }
        }

        private void ExecuteSaveCommand()
        {
            try
            {
                if (AddOrUpdatePatient())
                {
                    if (IsNewData)
                    {
                        ResetProperties();
                    }

                    RequestDisplayMessage("信息保存成功");
                }
            }
            catch (Exception ex)
            {
                RequestDisplayErrorMessage(ex.Message);
            }
        }

        private bool CanExecuteSaveCommand()
        {
            return CanExecuteCommand();
        }

        /// <summary>
        /// Register UpdatePatientDataMessage from PatientViewModel.
        /// </summary>
        private void RegisterUpdatePatientDataMessage()
        {
            Messenger.Default.Register<PatientViewModel>(this, PatientViewModel.UpdatePatientDataMessageToken, p =>
            {
                SetPatientProperties(p);
            });
        }
    }
}